// pages/cardDetail/cardDetail.js
var app = getApp()
import util from '../../utils/util.js'

Page({
  data: {
    tabs: [ {title: '详情'}, {title: '图片'}, {title: '视频'}, {title: '360环绕展示'} ],
    activeTab: 0,
    artifactData: {},
    artifactImage: [],
    artifactVideo: [],
    imageList: [],
  },

  onLoad(options) {
    this.getArtifactInfo(options.artifactId)
    this.getArtifactImage(options.artifactId)
    this.getArtifactVideo(options.artifactId)
  },

  getArtifactInfo(id) {
    let getInfoUrl = "/admin-api/artifact/info/get"
    let getInfoBody = {
      id: id
    }
    app.func.req(getInfoUrl, getInfoBody, (res) => {
      this.setData({'artifactData': res.data})
      const date = new Date(res.data.createTime)
      const formattedTime = util.formatTime(date)
      this.setData({'artifactData.formattedTime': formattedTime})
    })
  },

  getArtifactImage(id) {
    let getImageUrl = "/admin-api/artifact/info/info-image/list-by-artifact-id"
    let getImageBody = {
      artifactId: id
    }
    app.func.req(getImageUrl, getImageBody, (res) => {
      let tempList = []
      for (let i = 0; i < res.data.length; i++) {
        tempList.push(res.data[i].imagePath)
      }
      this.setData({'artifactImage': res.data, 'imageList': tempList})
    })
  },

  getArtifactVideo(id) {
    let getVideoUrl = "/admin-api/artifact/info/info-video/list-by-artifact-id"
    let getVideoBody = {
      artifactId: id
    }
    app.func.req(getVideoUrl, getVideoBody, (res) => {
      this.setData({'artifactVideo': res.data})
    })
  },

  onTabClick(e) {
    const index = e.detail.index
    this.setData({ 
      activeTab: index 
    })
  },

  onChange(e) {
    const index = e.detail.index
    this.setData({ 
      activeTab: index 
    })
  },
  
  previewImage(e) {
    let index = e.currentTarget.id
    wx.previewImage({
      current: this.data.imageList[index],
      urls: this.data.imageList
    })
  },

  videoErrorCallback(e) {
    console.log('视频错误信息:')
    console.log(e.detail.errMsg)
  },

  start360Presentation(e) {
    const artifactId = e.currentTarget.id
    wx.navigateTo({
      url: '../webView360/webView360?artifactId=' + artifactId,
    })
  },
})